﻿using Luna小店.service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Luna小店.生产工单
{
    public partial class FrmCADFiles : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public String NAME { get; set; }//目录名称
        public String ID { get; set; }//目录id
        public String FILE_ID { get; set; }//文件id
        public String FILE_NAME { get; set; }//文件名称
        public String STYLE_FIT_CD { get; set; }//版型CD
        public FrmCADFiles()
        {
            InitializeComponent();
        }

        private void FrmCADFiles_Load(object sender, EventArgs e)
        {
            this.RefreshTreeList();
            this.gridView文件一览.GroupPanelText = "当前目录：";
        }
        /// <summary>
        /// 刷新treelist
        /// </summary>
        private void RefreshTreeList()
        {
            this.treeList文件夹.DataSource = CADService.GetFilesIndex();
            this.treeList文件夹.Refresh();
        }
        /// <summary>
        /// 刷新文件一览
        /// </summary>
        private void Refresh文件一览()
        {
            this.gridControl1.DataSource = CADService.GetFilesByPID(this.ID);
            this.gridView文件一览.GroupPanelText = "当前目录：" + this.NAME;
            this.gridControl1.Refresh();
        }

        private void treeList文件夹_RowCellClick(object sender, DevExpress.XtraTreeList.RowCellClickEventArgs e)
        {
            this.ID = this.treeList文件夹.FocusedNode.GetValue("ID").ToString();
            this.NAME = this.treeList文件夹.FocusedNode.GetValue("NAME").ToString();
            this.Refresh文件一览();
        }

        private void repositoryItemButtonEdit更新_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (CADService.UpdataCADFile(this.gridView文件一览.GetDataRow(this.gridView文件一览.FocusedRowHandle)["ID"].ToString(), ofd.FileName, Path.GetFileName(ofd.FileName)) == true)
                {
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show("更新失败");
                }
                this.RefreshTreeList();
                this.Refresh文件一览();
            }
        }

        private void barButtonItem新增目录_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Frm目录名称(this.ID).ShowDialog();
            this.RefreshTreeList();
        }

        private void barButtonItem新增文件_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                new Frm版型名称(this.STYLE_FIT_CD).ShowDialog();
                this.STYLE_FIT_CD = "FIT_BODY_TYPE-" + this.STYLE_FIT_CD;
                if (CADService.CreateFile(ofd.FileName, Path.GetFileName(ofd.FileName), this.ID, "1",this.STYLE_FIT_CD) == true)
                {
                    MessageBox.Show("上传成功");
                }
                else
                {
                    MessageBox.Show("上传失败");
                }
                this.RefreshTreeList();
                this.Refresh文件一览();
            }
        }

        private void repositoryItemButtonEdit下载_Click(object sender, EventArgs e)
        {
            this.FILE_ID = this.gridView文件一览.GetDataRow(this.gridView文件一览.FocusedRowHandle)["ID"].ToString();
            this.FILE_NAME = this.gridView文件一览.GetDataRow(this.gridView文件一览.FocusedRowHandle)["CAD_FILE_NAME"].ToString();
            
            FolderBrowserDialog sFD = new FolderBrowserDialog();
            //saveFileDialog1.Title = "另存为";
            DialogResult rdr = sFD.ShowDialog();
            string fileName = sFD.SelectedPath;

            if (rdr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(fileName))
            {
                String fileIOStream = CADService.GetFileDataFlow(this.FILE_ID);
                try
                {
                    FileBinaryConvertHelper.StringToPath(fileIOStream, fileName + "\\" + this.FILE_NAME);
                    MessageBox.Show("下载成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("下载失败" + ex.Message);
                }
            }

        }

        private void treeList文件夹_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if(e.Column==colFILE_FLAG)
            {
                if (e.CellValue.ToString() == "1")
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.Options.UseBackColor = true;
                    e.Appearance.ForeColor = Color.Yellow;
                    e.Appearance.Options.UseForeColor = true;
                }
            }
        }

        private void repositoryItemButtonEdit标准款一览_Click(object sender, EventArgs e)
        {
            new Frm版型款式一览(this.gridView文件一览.GetDataRow(this.gridView文件一览.FocusedRowHandle)["STYLE_FIT_CD"].ToString()).ShowDialog();
        }
    }
}
